package com.app.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.app.common.Result;
import com.app.mapper.DistributionRecordsMapper;
import com.app.model.DistributionRecord;
import com.app.service.IDistributionRecordsService;
import com.app.utils.CrmebDateUtil;
import com.app.utils.DateLimitUtilVo;
import com.app.vo.DistributionRecordVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 发放记录 服务实现类
 * </p>
 *
 * @author yoominic
 * @since 2024-06-22
 */
@Service
public class DistributionRecordsServiceImpl extends ServiceImpl<DistributionRecordsMapper, DistributionRecord> implements IDistributionRecordsService {
    @Autowired
    private DistributionRecordsMapper distributionRecordsMapper;

    @Override
    public DistributionRecordVo getDistributionRecordsByBusinessId(Integer page, Integer limit, String dateLimit, Integer business, Integer integralDistributionType) throws Exception {
        Page<DistributionRecord> page1 = new Page<>(page, limit);
        LambdaQueryWrapper<DistributionRecord> lqw = Wrappers.lambdaQuery();
        lqw.eq(DistributionRecord::getBusinessId, business);
        lqw.eq(DistributionRecord::getType, 1);
        if (integralDistributionType != null) {
            lqw.like(DistributionRecord::getIntegralDistributionType, integralDistributionType);
        }
        if (StrUtil.isNotEmpty(dateLimit)) {
            DateLimitUtilVo dateLimitVo = CrmebDateUtil.getDateLimit(dateLimit);
            String startDate = DateUtil.parse(dateLimitVo.getStartTime()).toDateStr();
            String endDate = DateUtil.parse(dateLimitVo.getEndTime()).toDateStr();
            lqw.between(DistributionRecord::getDistributionTime, startDate, endDate);
        }
        lqw.orderByDesc(DistributionRecord::getDistributionTime);
        Page<DistributionRecord> selectedPage = distributionRecordsMapper.selectPage(page1, lqw);
        List<DistributionRecord> distributionRecordList = selectedPage.getRecords();
        for (DistributionRecord distributionRecord : distributionRecordList) {
            if (distributionRecord.getAmountDistributionType() != null) {
                if (distributionRecord.getAmountDistributionType() == 1) {
                    distributionRecord.setAmountDistributionTypeName("发放");
                } else if (distributionRecord.getAmountDistributionType() == 2) {
                    distributionRecord.setAmountDistributionTypeName("扣减");
                }
            }
            if (distributionRecord.getIntegralDistributionType() != null) {
                if (distributionRecord.getIntegralDistributionType() == 1) {
                    distributionRecord.setIntegralDistributionTypeName("发放");
                } else if (distributionRecord.getIntegralDistributionType() == 2) {
                    distributionRecord.setIntegralDistributionTypeName("扣减");
                }
            }
        }
        DistributionRecordVo vo = new DistributionRecordVo();
        vo.setList(distributionRecordList);
        vo.setCount((int) selectedPage.getTotal());
        return vo;
    }

    /**
     * 根据businessId删除发放记录
     *
     * @param id
     * @return
     */
    @Override
    public Result deleteDistributionRecordsById(Integer id) {
        distributionRecordsMapper.deleteById(id);
        return Result.success(200);
    }


}
